﻿// Copyright: 2012 
// Author: Minh Vu - YoungJ
// File name: FileSizeConverter.cs
// Solution: WpfEncryption
// Project: WpfEncryption
// Time: 11:51 PM 31/05/2012

using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Data;
using WpfEncryption.Hash_Algorithm;

namespace WpfEncryption.Converter
{
    public class FileSizeConverter : IValueConverter
    {
        #region Implementation of IValueConverter

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                var filename = value.ToString();
                if (string.IsNullOrEmpty(filename)) return string.Empty;
                var f = new FileInfo(filename);
                return !f.Exists ? "not is file" :
                    string.Format("File size: {0} ", f.Length.ToFileSize());
            }
            catch (Exception exception)
            {
                Debug.WriteLine("File size converter: "+exception.Message);
                return "not is file";
            }
            
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }

        #endregion
    }
}